﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using System.IO;
using ErpCoreModel;
using ErpCoreModel.Base;
using ErpCoreModel.Framework;
using ErpCore.Window;

namespace ErpCore.Database.Table
{
    public partial class MakeModelForm : Form
    {
        public List<CTable> m_lstTable = new List<CTable>();

        public string m_sNamespace = "SubSystem";
        public MakeModelForm()
        {
            InitializeComponent();
        }

        private void MakeModelForm_Load(object sender, EventArgs e)
        {
            LoadTable();
            txtNameSpace.Text = "ErpCoreModel.SubSystem";
            foreach (CTable table in m_lstTable)
            {
                if (table.Code.IndexOf("_") > 0)
                {
                    string sName = table.Code.Substring(0, table.Code.IndexOf("_"));
                    txtNameSpace.Text = "ErpCoreModel." + sName;
                    m_sNamespace = sName;
                    listPrefix.Items.Add(sName+"_");
                    break;
                }
            }
            MakeModel();
        }
        void LoadTable()
        {
            listTable.Items.Clear();
            foreach (CTable table in m_lstTable)
            {
                listTable.Items.Add(table.Code);
            }
        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            AddPrefixForm frm = new AddPrefixForm();
            if (frm.ShowDialog() != DialogResult.OK)
                return;
            string sPrefix = frm.txtPrefix.Text.Trim();
            for (int i = 0; i < listPrefix.Items.Count; i++)
            {
                if (listPrefix.Items[i].ToString().Equals(sPrefix, StringComparison.OrdinalIgnoreCase))
                {
                    MessageBox.Show("前缀已经存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            listPrefix.Items.Add(sPrefix);

            MakeModel();
        }

        private void btDel_Click(object sender, EventArgs e)
        {
            if (listPrefix.SelectedItems.Count == 0)
            {
                MessageBox.Show("请选择一项！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            for (int i = listPrefix.SelectedItems.Count - 1; i >= 0; i--)
            {
                listPrefix.Items.Remove(listPrefix.SelectedItems[i]);
            }

            MakeModel();
        }

        void MakeModel()
        {
            MakeModel(false,"");
        }
        void MakeModel(bool bMakeFile,string sDir)
        {
            listClass.Items.Clear();


            foreach (CTable table in m_lstTable)
            {
                string sCode = table.Code;
                for (int i = 0; i < listPrefix.Items.Count; i++)
                {
                    string pre = listPrefix.Items[i].ToString();
                    if (sCode.Length > pre.Length)
                    {
                        if (sCode.Substring(0, pre.Length).Equals(pre, StringComparison.OrdinalIgnoreCase))
                        {
                            sCode = sCode.Substring(pre.Length);
                        }
                    }
                    //sCode = sCode.TrimStart(listPrefix.Items[i].ToString().ToCharArray());

                }

                if (rd4.Checked)//ios
                {
                    string sExt = ".h";
                    string sClsName = "C" + sCode;
                    listClass.Items.Add(sClsName + sExt);
                    sExt = ".m";
                    listClass.Items.Add(sClsName + sExt);
                    sExt = ".h";
                    string sClsMgrName = sClsName + "Mgr";
                    listClass.Items.Add(sClsMgrName + sExt);
                    sExt = ".m";
                    listClass.Items.Add(sClsMgrName + sExt);

                    if (bMakeFile)
                    {
                        MakeModel_ios(sDir, table, sCode);
                    }
                }
                else if (rd3.Checked) //android
                {
                    string sExt = ".java";
                    string sClsName = "C" + sCode;
                    listClass.Items.Add(sClsName + sExt);
                    string sClsMgrName = sClsName + "Mgr";
                    listClass.Items.Add(sClsMgrName + sExt);

                    if (bMakeFile)
                    {
                        MakeModel_android(sDir, table, sCode);
                    }
                }
                else
                {
                    string sExt = ".cs";
                    if (rd2.Checked)
                        sExt = ".java";

                    string sClsName = "C" + sCode;
                    listClass.Items.Add(sClsName + sExt);
                    string sClsMgrName = sClsName + "Mgr";
                    listClass.Items.Add(sClsMgrName + sExt);

                    //对象
                    if (bMakeFile)
                    {
                        string sFile = sDir + sClsName + sExt;
                        File.Delete(sFile);
                        string sAttr = MakeAttrContent(table);

                        string sTemp = sTemplete;
                        if (rd2.Checked)
                            sTemp = sTemplete_java;
                        string sContent = string.Format(sTemp, sClsName, DateTime.Now.ToString(), txtNameSpace.Text.Trim(), table.Code, sAttr);
                        if (rd2.Checked)//java用utf-8
                            File.WriteAllText(sFile, sContent, Encoding.UTF8);
                        else
                            File.WriteAllText(sFile, sContent);
                    }
                    //对象管理器
                    if (bMakeFile)
                    {
                        string sFile = sDir + sClsMgrName + sExt;
                        File.Delete(sFile);

                        string sTemp = sTempleteMgr;
                        if (rd2.Checked)
                            sTemp = sTempleteMgr_java;
                        string sContent = string.Format(sTemp, sClsName, DateTime.Now.ToString(), txtNameSpace.Text.Trim(), table.Code);
                        if (rd2.Checked)//java用utf-8
                            File.WriteAllText(sFile, sContent, Encoding.UTF8);
                        else
                            File.WriteAllText(sFile, sContent);
                    }
                }
            }
        }

        string MakeAttrContent(CTable table)
        {
            string sRet = "";
            List<CBaseObject> lstObj = table.ColumnMgr.GetList();
            foreach (CBaseObject obj in lstObj)
            {
                CColumn col = (CColumn)obj;
                if (col.Code.Equals("id", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("created", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("creator", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updated", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updator", StringComparison.OrdinalIgnoreCase))
                    continue;

                string F = col.Code[0].ToString();
                string sAttrName = F.ToUpper()+col.Code.Substring(1);
                string sAttrKey = col.Code.ToLower();

                switch (col.ColType)
                {
                    case ColumnType.guid_type:
                    case ColumnType.ref_type:
                        {
                            string sTemp = sTAttr2;
                            if (rd2.Checked)
                                sTemp = sTAttr2_java;
                            sRet += string.Format(sTemp, sAttrName, sAttrKey);
                            break;
                        }
                    case ColumnType.string_type:
                    case ColumnType.text_type:
                    case ColumnType.path_type:
                        {
                            string sTemp = sTAttr1;
                            if (rd2.Checked)
                                sTemp = sTAttr1_java;
                            sRet += string.Format(sTemp, sAttrName, sAttrKey);
                            break;
                        }
                    case ColumnType.enum_type:
                        {
                            if (col.RefShowCol != Guid.Empty)
                            {
                                string sTemp = sTAttr2;
                                if (rd2.Checked)
                                    sTemp = sTAttr2_java;
                                sRet += string.Format(sTemp, sAttrName, sAttrKey);
                            }
                            else
                            {
                                string sTemp = sTAttr1;
                                if (rd2.Checked)
                                    sTemp = sTAttr1_java;
                                sRet += string.Format(sTemp, sAttrName, sAttrKey);
                            }
                            break;
                        }
                    case ColumnType.int_type:
                        {
                            string sTemp = sTAttr3;
                            if (rd2.Checked)
                                sTemp = sTAttr3_java;
                            sRet += string.Format(sTemp, sAttrName, sAttrKey);
                            break;
                        }
                    case ColumnType.long_type:
                        {
                            string sTemp = sTAttr4;
                            if (rd2.Checked)
                                sTemp = sTAttr4_java;
                            sRet += string.Format(sTemp, sAttrName, sAttrKey);
                            break;
                        }
                    case ColumnType.numeric_type:
                        {
                            string sTemp = sTAttr5;
                            if (rd2.Checked)
                                sTemp = sTAttr5_java;
                            sRet += string.Format(sTemp, sAttrName, sAttrKey);
                            break;
                        }
                    case ColumnType.datetime_type:
                        {
                            string sTemp = sTAttr7;
                            if (rd2.Checked)
                                sTemp = sTAttr7_java;
                            sRet += string.Format(sTemp, sAttrName, sAttrKey);
                            break;
                        }
                    case ColumnType.bool_type:
                        {
                            string sTemp = sTAttr6;
                            if (rd2.Checked)
                                sTemp = sTAttr6_java;
                            sRet += string.Format(sTemp, sAttrName, sAttrKey);
                            break;
                        }
                    case ColumnType.object_type:
                        {
                            string sTemp = sTAttr8;
                            if (rd2.Checked)
                                sTemp = sTAttr8_java;
                            sRet += string.Format(sTemp, sAttrName, sAttrKey);
                            break;
                        }
                }
            }
            return sRet;
        }

        //生成android 代码
        void MakeModel_android(string sDir, CTable table, string sCode)
        {
            //复制共用文件
            string sCommonDir = sDir;
            if (sCommonDir[sCommonDir.Length - 1] != '\\')
                sCommonDir += "\\";
            sCommonDir += "common\\";
            if (!Directory.Exists(sCommonDir))
                Directory.CreateDirectory(sCommonDir);
            string sSrcDir = Application.StartupPath + "\\android\\common\\";
            DirectoryInfo di = new DirectoryInfo(sSrcDir);
            FileInfo[] fis = di.GetFiles();
            foreach (FileInfo fi in fis)
            {
                //因为可能生成多个对象，所以文件不能覆盖
                if(!File.Exists(sCommonDir + fi.Name))
                    fi.CopyTo(sCommonDir + fi.Name);
            }
            //添加创建表代码
            string sContent = string.Format("// {0}\r\n",table.Name);
		    List<CBaseObject> lstCols = table.ColumnMgr.GetList();
            string sFields = "";
            foreach (CBaseObject colPO in lstCols)
            {
                CColumn col = (CColumn)colPO;

                if (col.Code.Equals("Id", StringComparison.OrdinalIgnoreCase))
                {
                    sFields += "[Id] varchar(50) PRIMARY KEY ,";
                    continue;
                }
                else if (col.Code.Equals("Created", StringComparison.OrdinalIgnoreCase))
                {
                    sFields += " [Created] long  ,";
                    continue;
                }
                else if (col.Code.Equals("Creator", StringComparison.OrdinalIgnoreCase))
                {
                    sFields += " [Creator] varchar(50) ";
                    sFields += " COLLATE NOCASE ,";
                    continue;
                }
                else if (col.Code.Equals("Updated", StringComparison.OrdinalIgnoreCase))
                {
                    sFields += " [Updated] long ,";
                    continue;
                }
                else if (col.Code.Equals("Updator", StringComparison.OrdinalIgnoreCase))
                {
                    sFields += " [Updator] varchar(50) ";
                    sFields += " COLLATE NOCASE ,";
                    continue;
                }
                string sDefault = (col.DefaultValue == "") ? "" : string.Format(" DEFAULT {0} ", col.DefaultValue);
                switch (col.ColType)
                {
                    case ColumnType.guid_type:
                    case ColumnType.ref_type:
                        sFields += string.Format(" [{0}] varchar(50) {1} NULL ", col.Code, sDefault);
                        sFields += " COLLATE NOCASE ,";
                        break;
                    case ColumnType.string_type:
                    case ColumnType.enum_type:
                    case ColumnType.path_type:
                        sFields += string.Format("[{0}] [varchar]({1}) {2} NULL ", col.Code, col.ColLen, sDefault);
                        sFields += " COLLATE NOCASE ,";
                        break;
                    case ColumnType.text_type:
                        sFields += string.Format("[{0}] [text]  {1} NULL ,", col.Code, sDefault);
                        break;
                    case ColumnType.int_type:
                        sFields += string.Format("[{0}] [int]  {1} NULL ,", col.Code, sDefault);
                        break;
                    case ColumnType.long_type:
                        sFields += string.Format("[{0}] [bigint]  {1} NULL ,", col.Code, sDefault);
                        break;
                    case ColumnType.bool_type:
                        sFields += string.Format("[{0}] [bit]  {1} NULL ,", col.Code, sDefault);
                        break;
                    case ColumnType.datetime_type:
                        sFields += string.Format("[{0}] [long]  {1} NULL ,", col.Code, sDefault);
                        break;
                    case ColumnType.numeric_type:
                        sFields += string.Format("[{0}] [numeric]({1},{2})  {3} NULL ,", col.Code, col.ColLen, col.ColDecimal, sDefault);
                        break;
                    case ColumnType.object_type:
                        sFields += string.Format("[{0}] [image]  {1} NULL ,", col.Code, sDefault);
                        break;
                }
            }
            sFields = sFields.TrimEnd(',');

            string sCreateTable = string.Format(@"CREATE TABLE [{0}] ({1})",
                   table.Code,
                   sFields);
            sContent += string.Format("sqlBuilder=\"{0}\";\r\n", sCreateTable);
            sContent += "db.execSQL(sqlBuilder);\r\n";

            string sFile = sCommonDir + "DatabaseHelper.java";
            if (!File.Exists(sFile))
            {
                MessageBox.Show(string.Format("缺少文件：{0}",sFile));
                return;
            }
            string sTemplate = File.ReadAllText(sFile);
            sContent = sTemplate.Replace("//[CREATE SQL]", sContent + "\r\n//[CREATE SQL]\r\n");
            File.WriteAllText(sFile, sContent);

            string sExt = ".java";
            string sClsName = "C" + sCode;
            string sClsMgrName = sClsName + "Mgr";
            //对象
            sFile = Application.StartupPath + "\\android\\template\\template.java";
            sTemplate = File.ReadAllText(sFile);
            sContent = string.Format(sTemplate, txtNameSpace.Text, sClsName, MakeTemplate_android_2(table), MakeTemplate_android_3(table), table.Code, MakeTemplate_android_5(table), MakeTemplate_android_6(table), MakeTemplate_android_7(table), MakeTemplate_android_8(table));
            string sModelDir = sDir;
            if (sModelDir[sModelDir.Length - 1] != '\\')
                sModelDir += "\\";
            sModelDir += "Model\\";
            if (!Directory.Exists(sModelDir))
                Directory.CreateDirectory(sModelDir);
            sFile = sModelDir + sClsName + sExt;
            File.WriteAllText(sFile, sContent);
            //对象管理器
            sFile = Application.StartupPath + "\\android\\template\\templatemgr.java";
            sTemplate = File.ReadAllText(sFile);
            sContent = string.Format(sTemplate, txtNameSpace.Text, sClsName, table.Code, MakeTemplateMgr_android_3(table), MakeTemplateMgr_android_4(table));

            sFile = sModelDir + sClsMgrName + sExt;
            File.WriteAllText(sFile, sContent);
        }
        string MakeTemplate_android_2(CTable table)
        {
            string sRet = "";
            List<CBaseObject> lstObj = table.ColumnMgr.GetList();
            foreach (CBaseObject obj in lstObj)
            {
                CColumn col = (CColumn)obj;
                if (col.Code.Equals("id", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("created", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("creator", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updated", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updator", StringComparison.OrdinalIgnoreCase))
                    continue;

                
                switch (col.ColType)
                {
                    case ColumnType.guid_type:
                    case ColumnType.ref_type:
                        {
                            sRet += string.Format("public UUID {0};\r\n", col.Code);
                            break;
                        }
                    case ColumnType.string_type:
                    case ColumnType.text_type:
                    case ColumnType.path_type:
                        {
                            sRet += string.Format("public String {0};\r\n", col.Code);
                            break;
                        }
                    case ColumnType.enum_type:
                        {
                            sRet += string.Format("public String {0};\r\n", col.Code);
                            break;
                        }
                    case ColumnType.int_type:
                        {
                            sRet += string.Format("public int {0};\r\n", col.Code);
                            break;
                        }
                    case ColumnType.long_type:
                        {
                            sRet += string.Format("public long {0};\r\n", col.Code);
                            break;
                        }
                    case ColumnType.numeric_type:
                        {
                            sRet += string.Format("public double {0};\r\n", col.Code);
                            break;
                        }
                    case ColumnType.datetime_type:
                        {
                            sRet += string.Format("public Date {0};\r\n", col.Code);
                            break;
                        }
                    case ColumnType.bool_type:
                        {
                            sRet += string.Format("public Boolean {0};\r\n", col.Code);
                            break;
                        }
                    case ColumnType.object_type:
                        {
                            sRet += string.Format("public Object {0};\r\n", col.Code);
                            break;
                        }
                }
            }
            return sRet;
        }
        string MakeTemplate_android_3(CTable table)
        {
            string sRet = "";
            List<CBaseObject> lstObj = table.ColumnMgr.GetList();
            foreach (CBaseObject obj in lstObj)
            {
                CColumn col = (CColumn)obj;
                if (col.Code.Equals("id", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("created", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("creator", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updated", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updator", StringComparison.OrdinalIgnoreCase))
                    continue;

                switch (col.ColType)
                {
                    case ColumnType.guid_type:
                    case ColumnType.ref_type:
                        {
                            sRet += string.Format("{0} = UUID.fromString(jsonobj.optString(\"{0}\"));\r\n", col.Code);
                            break;
                        }
                    case ColumnType.string_type:
                    case ColumnType.text_type:
                    case ColumnType.path_type:
                        {
                            sRet += string.Format("{0} = jsonobj.optString(\"{0}\");\r\n", col.Code);
                            break;
                        }
                    case ColumnType.enum_type:
                        {
                            sRet += string.Format("{0} = jsonobj.optString(\"{0}\");\r\n", col.Code);
                            break;
                        }
                    case ColumnType.int_type:
                        {
                            sRet += string.Format("{0} = jsonobj.optInt(\"{0}\");\r\n", col.Code);
                            break;
                        }
                    case ColumnType.long_type:
                        {
                            sRet += string.Format("{0} = jsonobj.optLong(\"{0}\");\r\n", col.Code);
                            break;
                        }
                    case ColumnType.numeric_type:
                        {
                            sRet += string.Format("{0} = jsonobj.optDouble(\"{0}\");\r\n", col.Code);
                            break;
                        }
                    case ColumnType.datetime_type:
                        {
                            string str = @" try{{
                                {0} = format.parse(jsonobj.optString(""{0}""));
                            }}
                            catch (ParseException e)
                            {{
                                e.printStackTrace();
                            }}";
                            sRet += string.Format(str, col.Code);
                            sRet += "\r\n";
                            break;
                        }
                    case ColumnType.bool_type:
                        {
                            sRet += string.Format("{0} = jsonobj.optBoolean(\"{0}\");\r\n", col.Code);
                            break;
                        }
                    case ColumnType.object_type:
                        {
                            sRet += string.Format("{0} = jsonobj.opt(\"{0}\");\r\n", col.Code);
                            break;
                        }
                }
            }
            return sRet;
        }
        string MakeTemplate_android_5(CTable table)
        {
            string sRet = "";
            List<CBaseObject> lstObj = table.ColumnMgr.GetList();
            foreach (CBaseObject obj in lstObj)
            {
                CColumn col = (CColumn)obj;
                if (col.Code.Equals("id", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("created", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("creator", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updated", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updator", StringComparison.OrdinalIgnoreCase))
                    continue;

                sRet += string.Format("[{0}],",col.Code);

            }
            sRet = sRet.TrimEnd(",".ToCharArray());
            return sRet;
        }
        string MakeTemplate_android_6(CTable table)
        {
            string sRet = "";
            List<CBaseObject> lstObj = table.ColumnMgr.GetList();
            foreach (CBaseObject obj in lstObj)
            {
                CColumn col = (CColumn)obj;
                if (col.Code.Equals("id", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("created", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("creator", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updated", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updator", StringComparison.OrdinalIgnoreCase))
                    continue;

                sRet += "?,";
            }
            sRet = sRet.TrimEnd(",".ToCharArray());
            return sRet;
        }
        string MakeTemplate_android_7(CTable table)
        {
            string sRet = "";
            List<CBaseObject> lstObj = table.ColumnMgr.GetList();
            foreach (CBaseObject obj in lstObj)
            {
                CColumn col = (CColumn)obj;
                if (col.Code.Equals("id", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("created", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("creator", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updated", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updator", StringComparison.OrdinalIgnoreCase))
                    continue;


                switch (col.ColType)
                {
                    case ColumnType.guid_type:
                    case ColumnType.ref_type:
                        {
                            sRet += string.Format("{0},", col.Code);
                            break;
                        }
                    case ColumnType.string_type:
                    case ColumnType.text_type:
                    case ColumnType.path_type:
                        {
                            sRet += string.Format("{0},", col.Code);
                            break;
                        }
                    case ColumnType.enum_type:
                        {
                            sRet += string.Format("{0},", col.Code);
                            break;
                        }
                    case ColumnType.int_type:
                        {
                            sRet += string.Format("{0},", col.Code);
                            break;
                        }
                    case ColumnType.long_type:
                        {
                            sRet += string.Format("{0},", col.Code);
                            break;
                        }
                    case ColumnType.numeric_type:
                        {
                            sRet += string.Format("{0},", col.Code);
                            break;
                        }
                    case ColumnType.datetime_type:
                        {
                            sRet += string.Format("{0}.getTime(),", col.Code);
                            break;
                        }
                    case ColumnType.bool_type:
                        {
                            sRet += string.Format("{0},", col.Code);
                            break;
                        }
                    case ColumnType.object_type:
                        {
                            sRet += string.Format("{0},", col.Code);
                            break;
                        }
                }
            }
            sRet = sRet.TrimEnd(",".ToCharArray());
            return sRet;
        }
        string MakeTemplate_android_8(CTable table)
        {
            string sRet = "";
            List<CBaseObject> lstObj = table.ColumnMgr.GetList();
            foreach (CBaseObject obj in lstObj)
            {
                CColumn col = (CColumn)obj;
                if (col.Code.Equals("id", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("created", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("creator", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updated", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updator", StringComparison.OrdinalIgnoreCase))
                    continue;

                sRet += string.Format("{0}=?,",col.Code);

            }
            sRet = sRet.TrimEnd(",".ToCharArray());
            return sRet;
        }
        string MakeTemplateMgr_android_3(CTable table)
        {
            string sRet = "";
            List<CBaseObject> lstObj = table.ColumnMgr.GetList();
            foreach (CBaseObject obj in lstObj)
            {
                CColumn col = (CColumn)obj;
                if (col.Code.Equals("id", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("created", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("creator", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updated", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updator", StringComparison.OrdinalIgnoreCase))
                    continue;


                switch (col.ColType)
                {
                    case ColumnType.guid_type:
                    case ColumnType.ref_type:
                        {
                            sRet += string.Format("obj.{0} = UUID.fromString(cursor.getString(cursor.getColumnIndex(\"{0}\")));\r\n", col.Code);
                            break;
                        }
                    case ColumnType.string_type:
                    case ColumnType.text_type:
                    case ColumnType.path_type:
                        {
                            sRet += string.Format("obj.{0} = cursor.getString(cursor.getColumnIndex(\"{0}\"));\r\n", col.Code);
                            break;
                        }
                    case ColumnType.enum_type:
                        {
                            sRet += string.Format("obj.{0} = cursor.getString(cursor.getColumnIndex(\"{0}\"));\r\n", col.Code);
                            break;
                        }
                    case ColumnType.int_type:
                        {
                            sRet += string.Format("obj.{0} = cursor.getInt(cursor.getColumnIndex(\"{0}\"));\r\n", col.Code);
                            break;
                        }
                    case ColumnType.long_type:
                        {
                            sRet += string.Format("obj.{0} = cursor.getLong(cursor.getColumnIndex(\"{0}\"));\r\n", col.Code);
                            break;
                        }
                    case ColumnType.numeric_type:
                        {
                            sRet += string.Format("obj.{0} = cursor.getDouble(cursor.getColumnIndex(\"{0}\"));\r\n", col.Code);
                            break;
                        }
                    case ColumnType.datetime_type:
                        {
                            sRet += string.Format("obj.{0} = new Date(cursor.getLong(cursor.getColumnIndex(\"{0}\")));\r\n", col.Code);
                            break;
                        }
                    case ColumnType.bool_type:
                        {
                            sRet += string.Format("obj.{0} = cursor.getInt(cursor.getColumnIndex(\"{0}\"))==1?true:false;\r\n", col.Code);
                            break;
                        }
                    case ColumnType.object_type:
                        {
                            sRet += string.Format("obj.{0} = cursor.get(cursor.getColumnIndex(\"{0}\"));\r\n", col.Code);
                            break;
                        }
                }
            }
            return sRet;
        }
        string MakeTemplateMgr_android_4(CTable table)
        {
            string sRet = "";
            List<CBaseObject> lstObj = table.ColumnMgr.GetList();
            foreach (CBaseObject obj in lstObj)
            {
                CColumn col = (CColumn)obj;
                if (col.Code.Equals("id", StringComparison.OrdinalIgnoreCase))
                    continue;

                sRet += string.Format("obj.{0} = obj2.{0};\r\n", col.Code);                    
            }
            return sRet;
        }
        //生成ios 代码
        void MakeModel_ios(string sDir, CTable table, string sCode)
        {
            //复制共用文件
            string sCommonDir = sDir;
            if (sCommonDir[sCommonDir.Length - 1] != '\\')
                sCommonDir += "\\";
            sCommonDir += "common\\";
            if (!Directory.Exists(sCommonDir))
                Directory.CreateDirectory(sCommonDir);
            string sSrcDir = Application.StartupPath + "\\ios\\common\\";
            DirectoryInfo di = new DirectoryInfo(sSrcDir);
            FileInfo[] fis = di.GetFiles();
            foreach (FileInfo fi in fis)
            {
                //因为可能生成多个对象，所以文件不能覆盖
                if (!File.Exists(sCommonDir + fi.Name))
                    fi.CopyTo(sCommonDir + fi.Name);
            }
            //添加创建表代码
            string sContent = string.Format("// {0}\r\n", table.Name);
            List<CBaseObject> lstCols = table.ColumnMgr.GetList();
            string sFields = "";
            foreach (CBaseObject colPO in lstCols)
            {
                CColumn col = (CColumn)colPO;

                if (col.Code.Equals("Id", StringComparison.OrdinalIgnoreCase))
                {
                    sFields += "[Id] varchar(50) PRIMARY KEY ,";
                    continue;
                }
                else if (col.Code.Equals("Created", StringComparison.OrdinalIgnoreCase))
                {
                    sFields += " [Created] long  ,";
                    continue;
                }
                else if (col.Code.Equals("Creator", StringComparison.OrdinalIgnoreCase))
                {
                    sFields += " [Creator] varchar(50) ";
                    sFields += " COLLATE NOCASE ,";
                    continue;
                }
                else if (col.Code.Equals("Updated", StringComparison.OrdinalIgnoreCase))
                {
                    sFields += " [Updated] long ,";
                    continue;
                }
                else if (col.Code.Equals("Updator", StringComparison.OrdinalIgnoreCase))
                {
                    sFields += " [Updator] varchar(50) ";
                    sFields += " COLLATE NOCASE ,";
                    continue;
                }
                string sDefault = (col.DefaultValue == "") ? "" : string.Format(" DEFAULT {0} ", col.DefaultValue);
                switch (col.ColType)
                {
                    case ColumnType.guid_type:
                    case ColumnType.ref_type:
                        sFields += string.Format(" [{0}] varchar(50) {1} NULL ", col.Code, sDefault);
                        sFields += " COLLATE NOCASE ,";
                        break;
                    case ColumnType.string_type:
                    case ColumnType.enum_type:
                    case ColumnType.path_type:
                        sFields += string.Format("[{0}] [varchar]({1}) {2} NULL ", col.Code, col.ColLen, sDefault);
                        sFields += " COLLATE NOCASE ,";
                        break;
                    case ColumnType.text_type:
                        sFields += string.Format("[{0}] [text]  {1} NULL ,", col.Code, sDefault);
                        break;
                    case ColumnType.int_type:
                        sFields += string.Format("[{0}] [int]  {1} NULL ,", col.Code, sDefault);
                        break;
                    case ColumnType.long_type:
                        sFields += string.Format("[{0}] [bigint]  {1} NULL ,", col.Code, sDefault);
                        break;
                    case ColumnType.bool_type:
                        sFields += string.Format("[{0}] [bit]  {1} NULL ,", col.Code, sDefault);
                        break;
                    case ColumnType.datetime_type:
                        sFields += string.Format("[{0}] [long]  {1} NULL ,", col.Code, sDefault);
                        break;
                    case ColumnType.numeric_type:
                        sFields += string.Format("[{0}] [numeric]({1},{2})  {3} NULL ,", col.Code, col.ColLen, col.ColDecimal, sDefault);
                        break;
                    case ColumnType.object_type:
                        sFields += string.Format("[{0}] [image]  {1} NULL ,", col.Code, sDefault);
                        break;
                }
            }
            sFields = sFields.TrimEnd(',');

            string sCreateTable = string.Format(@"CREATE TABLE [{0}] ({1})",
                   table.Code,
                   sFields);
            sContent += string.Format("sqlBuilder=@\"{0}\"\r\n", sCreateTable);
            sContent += "[DB executeUpdate:sqlBuilder];\r\n";

            string sFile = sCommonDir + "CContext.m";
            if (!File.Exists(sFile))
            {
                MessageBox.Show(string.Format("缺少文件：{0}", sFile));
                return;
            }
            string sTemplate = File.ReadAllText(sFile);
            sContent = sTemplate.Replace("//[CREATE SQL]", sContent + "\r\n//[CREATE SQL]\r\n");
            File.WriteAllText(sFile, sContent);

            string sExt = ".h";
            string sClsName = "C" + sCode;
            string sClsMgrName = sClsName + "Mgr";
            //对象
            sFile = Application.StartupPath + "\\ios\\template\\template.h";
            sTemplate = File.ReadAllText(sFile);
            sContent = string.Format(sTemplate, sClsName, table.Name, MakeTemplateH_ios_2(table), MakeTemplateH_ios_3(table));
            string sModelDir = sDir;
            if (sModelDir[sModelDir.Length - 1] != '\\')
                sModelDir += "\\";
            sModelDir += "Model\\";
            if (!Directory.Exists(sModelDir))
                Directory.CreateDirectory(sModelDir);
            sFile = sModelDir + sClsName + sExt;
            File.WriteAllText(sFile, sContent);

            sExt = ".m";
            sFile = Application.StartupPath + "\\ios\\template\\template.m";
            sTemplate = File.ReadAllText(sFile);
            sContent = string.Format(sTemplate, sClsName, MakeTemplateM_ios_1(table), MakeTemplateM_ios_2(table), table.Code, MakeTemplateM_ios_4(table), MakeTemplateM_ios_5(table), MakeTemplateM_ios_6(table), MakeTemplateM_ios_7(table));
            sFile = sModelDir + sClsName + sExt;
            File.WriteAllText(sFile, sContent);
            //对象管理器
            sExt = ".h";
            sFile = Application.StartupPath + "\\ios\\template\\templatemgr.h";
            sTemplate = File.ReadAllText(sFile);
            sContent = string.Format(sTemplate,  sClsName);
            sFile = sModelDir + sClsMgrName + sExt;
            File.WriteAllText(sFile, sContent);

            sExt = ".m";
            sFile = Application.StartupPath + "\\ios\\template\\templatemgr.m";
            sTemplate = File.ReadAllText(sFile);
            sContent = string.Format(sTemplate, sClsName, table.Code, MakeTemplateMgrM_ios_2(table), MakeTemplateMgrM_ios_3(table));
            sFile = sModelDir + sClsMgrName + sExt;
            File.WriteAllText(sFile, sContent);
        }
        string MakeTemplateH_ios_2(CTable table)
        {
            string sRet = "";
            List<CBaseObject> lstObj = table.ColumnMgr.GetList();
            foreach (CBaseObject obj in lstObj)
            {
                CColumn col = (CColumn)obj;
                if (col.Code.Equals("id", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("created", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("creator", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updated", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updator", StringComparison.OrdinalIgnoreCase))
                    continue;


                switch (col.ColType)
                {
                    case ColumnType.guid_type:
                    case ColumnType.ref_type:
                        {
                            sRet += string.Format("NSString *{0};\r\n", col.Code);
                            break;
                        }
                    case ColumnType.string_type:
                    case ColumnType.text_type:
                    case ColumnType.path_type:
                        {
                            sRet += string.Format("NSString *{0};\r\n", col.Code);
                            break;
                        }
                    case ColumnType.enum_type:
                        {
                            sRet += string.Format("NSString *{0};\r\n", col.Code);
                            break;
                        }
                    case ColumnType.int_type:
                        {
                            sRet += string.Format("int {0};\r\n", col.Code);
                            break;
                        }
                    case ColumnType.long_type:
                        {
                            sRet += string.Format("long {0};\r\n", col.Code);
                            break;
                        }
                    case ColumnType.numeric_type:
                        {
                            sRet += string.Format("double {0};\r\n", col.Code);
                            break;
                        }
                    case ColumnType.datetime_type:
                        {
                            sRet += string.Format("NSDate *{0};\r\n", col.Code);
                            break;
                        }
                    case ColumnType.bool_type:
                        {
                            sRet += string.Format("bool {0};\r\n", col.Code);
                            break;
                        }
                    case ColumnType.object_type:
                        {
                            sRet += string.Format("NSObject *{0};\r\n", col.Code);
                            break;
                        }
                }
            }
            return sRet;
        }
        string MakeTemplateH_ios_3(CTable table)
        {
            string sRet = "";
            List<CBaseObject> lstObj = table.ColumnMgr.GetList();
            foreach (CBaseObject obj in lstObj)
            {
                CColumn col = (CColumn)obj;
                if (col.Code.Equals("id", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("created", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("creator", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updated", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updator", StringComparison.OrdinalIgnoreCase))
                    continue;


                switch (col.ColType)
                {
                    case ColumnType.guid_type:
                    case ColumnType.ref_type:
                        {
                            sRet += string.Format("@property(nonatomic,copy) NSString *{0};\r\n", col.Code);
                            break;
                        }
                    case ColumnType.string_type:
                    case ColumnType.text_type:
                    case ColumnType.path_type:
                        {
                            sRet += string.Format("@property(nonatomic,copy) NSString *{0};\r\n", col.Code);
                            break;
                        }
                    case ColumnType.enum_type:
                        {
                            sRet += string.Format("@property(nonatomic,copy) NSString *{0};\r\n", col.Code);
                            break;
                        }
                    case ColumnType.int_type:
                        {
                            sRet += string.Format("@property(nonatomic,assign) int {0};\r\n", col.Code);
                            break;
                        }
                    case ColumnType.long_type:
                        {
                            sRet += string.Format("@property(nonatomic,assign) long {0};\r\n", col.Code);
                            break;
                        }
                    case ColumnType.numeric_type:
                        {
                            sRet += string.Format("@property(nonatomic,assign) double {0};\r\n", col.Code);
                            break;
                        }
                    case ColumnType.datetime_type:
                        {
                            sRet += string.Format("@property(nonatomic,retain) NSDate *{0};\r\n", col.Code);
                            break;
                        }
                    case ColumnType.bool_type:
                        {
                            sRet += string.Format("@property(nonatomic,assign) bool {0};\r\n", col.Code);
                            break;
                        }
                    case ColumnType.object_type:
                        {
                            sRet += string.Format("@property(nonatomic,retain) NSObject *{0};\r\n", col.Code);
                            break;
                        }
                }
            }
            return sRet;
        }
        string MakeTemplateM_ios_1(CTable table)
        {
            string sRet = "@synthesize ";
            List<CBaseObject> lstObj = table.ColumnMgr.GetList();
            foreach (CBaseObject obj in lstObj)
            {
                CColumn col = (CColumn)obj;
                if (col.Code.Equals("id", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("created", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("creator", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updated", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updator", StringComparison.OrdinalIgnoreCase))
                    continue;

                sRet += string.Format("{0},",col.Code);
            }
            sRet = sRet.TrimEnd(",".ToCharArray());
            return sRet;
        }
        string MakeTemplateM_ios_2(CTable table)
        {
            string sRet = "";
            List<CBaseObject> lstObj = table.ColumnMgr.GetList();
            foreach (CBaseObject obj in lstObj)
            {
                CColumn col = (CColumn)obj;
                if (col.Code.Equals("id", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("created", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("creator", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updated", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updator", StringComparison.OrdinalIgnoreCase))
                    continue;


                switch (col.ColType)
                {
                    case ColumnType.guid_type:
                    case ColumnType.ref_type:
                        {
                            sRet += string.Format("{0}=[json objectForKey:@\"{0}\"];\r\n", col.Code);
                            break;
                        }
                    case ColumnType.string_type:
                    case ColumnType.text_type:
                    case ColumnType.path_type:
                        {
                            sRet += string.Format("{0}=[json objectForKey:@\"{0}\"];\r\n", col.Code);
                            break;
                        }
                    case ColumnType.enum_type:
                        {
                            sRet += string.Format("{0}=[json objectForKey:@\"{0}\"];\r\n", col.Code);
                            break;
                        }
                    case ColumnType.int_type:
                        {
                            sRet += string.Format("{0}=[[json objectForKey:@\"{0}\"]intValue];\r\n", col.Code);
                            break;
                        }
                    case ColumnType.long_type:
                        {
                            sRet += string.Format("{0}=[[json objectForKey:@\"{0}\"]longValue];\r\n", col.Code);
                            break;
                        }
                    case ColumnType.numeric_type:
                        {
                            sRet += string.Format("{0}=[[json objectForKey:@\"{0}\"]doubleValue];\r\n", col.Code);
                            break;
                        }
                    case ColumnType.datetime_type:
                        {
                            sRet += string.Format("{0}=[dateFmt dateFromString:[json objectForKey:@\"{0}\"] ];\r\n", col.Code);
                            break;
                        }
                    case ColumnType.bool_type:
                        {
                            sRet += string.Format("{0}=[[json objectForKey:@\"{0}\"]boolValue];\r\n", col.Code);
                            break;
                        }
                    case ColumnType.object_type:
                        {
                            sRet += string.Format("{0}=[json objectForKey:@\"{0}\"];\r\n", col.Code);
                            break;
                        }
                }
            }
            return sRet;
        }
        string MakeTemplateM_ios_4(CTable table)
        {
            string sRet = "";
            List<CBaseObject> lstObj = table.ColumnMgr.GetList();
            foreach (CBaseObject obj in lstObj)
            {
                CColumn col = (CColumn)obj;
                if (col.Code.Equals("id", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("created", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("creator", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updated", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updator", StringComparison.OrdinalIgnoreCase))
                    continue;

                sRet += string.Format("{0},",col.Code);
            }
            sRet= sRet.TrimEnd(",".ToCharArray());
            return sRet;
        }
        string MakeTemplateM_ios_5(CTable table)
        {
            string sRet = "";
            List<CBaseObject> lstObj = table.ColumnMgr.GetList();
            foreach (CBaseObject obj in lstObj)
            {
                CColumn col = (CColumn)obj;
                if (col.Code.Equals("id", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("created", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("creator", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updated", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updator", StringComparison.OrdinalIgnoreCase))
                    continue;

                sRet += string.Format("?,");
            }
            sRet = sRet.TrimEnd(",".ToCharArray());
            return sRet;
        }
        string MakeTemplateM_ios_6(CTable table)
        {
            string sRet = "";
            List<CBaseObject> lstObj = table.ColumnMgr.GetList();
            foreach (CBaseObject obj in lstObj)
            {
                CColumn col = (CColumn)obj;
                if (col.Code.Equals("id", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("created", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("creator", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updated", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updator", StringComparison.OrdinalIgnoreCase))
                    continue;


                switch (col.ColType)
                {
                    case ColumnType.guid_type:
                    case ColumnType.ref_type:
                        {
                            sRet += string.Format("{0},", col.Code);
                            break;
                        }
                    case ColumnType.string_type:
                    case ColumnType.text_type:
                    case ColumnType.path_type:
                        {
                            sRet += string.Format("{0},", col.Code);
                            break;
                        }
                    case ColumnType.enum_type:
                        {
                            sRet += string.Format("{0},", col.Code);
                            break;
                        }
                    case ColumnType.int_type:
                        {
                            sRet += string.Format("[NSNumber numberWithInt:{0}],", col.Code);
                            break;
                        }
                    case ColumnType.long_type:
                        {
                            sRet += string.Format("[NSNumber numberWithLong:{0}],", col.Code);
                            break;
                        }
                    case ColumnType.numeric_type:
                        {
                            sRet += string.Format("[NSNumber numberWithDouble:{0}],", col.Code);
                            break;
                        }
                    case ColumnType.datetime_type:
                        {
                            sRet += string.Format("[NSNumber numberWithLongLong:(long long)[{0} timeIntervalSince1970]],", col.Code);
                            break;
                        }
                    case ColumnType.bool_type:
                        {
                            sRet += string.Format("[NSNumber numberWithBool:{0}],", col.Code);
                            break;
                        }
                    case ColumnType.object_type:
                        {
                            sRet += string.Format("{0},", col.Code);
                            break;
                        }
                }
            }
            sRet = sRet.TrimEnd(",".ToCharArray());
            return sRet;
        }
        string MakeTemplateM_ios_7(CTable table)
        {
            string sRet = "";
            List<CBaseObject> lstObj = table.ColumnMgr.GetList();
            foreach (CBaseObject obj in lstObj)
            {
                CColumn col = (CColumn)obj;
                if (col.Code.Equals("id", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("created", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("creator", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updated", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updator", StringComparison.OrdinalIgnoreCase))
                    continue;

                sRet += string.Format("{0}=?,", col.Code);
            }
            sRet = sRet.TrimEnd(",".ToCharArray());
            return sRet;
        }
        string MakeTemplateMgrM_ios_2(CTable table)
        {
            string sRet = "";
            List<CBaseObject> lstObj = table.ColumnMgr.GetList();
            foreach (CBaseObject obj in lstObj)
            {
                CColumn col = (CColumn)obj;
                if (col.Code.Equals("id", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("created", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("creator", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updated", StringComparison.OrdinalIgnoreCase))
                    continue;
                else if (col.Code.Equals("updator", StringComparison.OrdinalIgnoreCase))
                    continue;


                switch (col.ColType)
                {
                    case ColumnType.guid_type:
                    case ColumnType.ref_type:
                        {
                            sRet += string.Format("obj.{0}=[rs stringForColumn:@\"{0}\"];\r\n", col.Code);
                            break;
                        }
                    case ColumnType.string_type:
                    case ColumnType.text_type:
                    case ColumnType.path_type:
                        {
                            sRet += string.Format("obj.{0}=[rs stringForColumn:@\"{0}\"];\r\n", col.Code);
                            break;
                        }
                    case ColumnType.enum_type:
                        {
                            sRet += string.Format("obj.{0}=[rs stringForColumn:@\"{0}\"];\r\n", col.Code);
                            break;
                        }
                    case ColumnType.int_type:
                        {
                            sRet += string.Format("obj.{0}=[[rs stringForColumn:@\"{0}\"]intValue];\r\n", col.Code);
                            break;
                        }
                    case ColumnType.long_type:
                        {
                            sRet += string.Format("obj.{0}=[[rs stringForColumn:@\"{0}\"]longValue];\r\n", col.Code);
                            break;
                        }
                    case ColumnType.numeric_type:
                        {
                            sRet += string.Format("obj.{0}=[[rs stringForColumn:@\"{0}\"]doubleValue];\r\n", col.Code);
                            break;
                        }
                    case ColumnType.datetime_type:
                        {
                            sRet += string.Format("obj.{0}=[NSDate dateWithTimeIntervalSince1970:[rs longLongIntForColumn:@\"{0}\"] ];\r\n", col.Code);
                            break;
                        }
                    case ColumnType.bool_type:
                        {
                            sRet += string.Format("obj.{0}=[[rs stringForColumn:@\"{0}\"]boolValue];\r\n", col.Code);
                            break;
                        }
                    case ColumnType.object_type:
                        {
                            sRet += string.Format("obj.{0}=[rs objectForColumn:@\"{0}\"];\r\n", col.Code);
                            break;
                        }
                }
            }
            return sRet;
        }
        string MakeTemplateMgrM_ios_3(CTable table)
        {
            string sRet = "";
            List<CBaseObject> lstObj = table.ColumnMgr.GetList();
            foreach (CBaseObject obj in lstObj)
            {
                CColumn col = (CColumn)obj;
                if (col.Code.Equals("id", StringComparison.OrdinalIgnoreCase))
                    continue;

                sRet += string.Format("obj.{0}=obj2.{0};\r\n", col.Code);
               
            }
            return sRet;
        }

        private void btBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            if (dlg.ShowDialog() != DialogResult.OK)
                return;
            txtPath.Text = dlg.SelectedPath;
        }

        private void btOk_Click(object sender, EventArgs e)
        {
            if (txtPath.Text.Trim() == "")
            {
                MessageBox.Show("请选择生成路径！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            string sDir = txtPath.Text.Trim();
            if (sDir[sDir.Length - 1] != '\\')
                sDir += "\\";
            try
            {
                if (!Directory.Exists(sDir))
                    Directory.CreateDirectory(sDir);
            }
            catch
            {
                MessageBox.Show("创建路径失败！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            MakeModel(true,sDir);

            MessageBox.Show("生成完毕！");
            System.Diagnostics.Process.Start("explorer.exe ", sDir);
        }

        private void rd1_CheckedChanged(object sender, EventArgs e)
        {
            txtNameSpace.Text = "ErpCoreModel." + m_sNamespace;
            
            MakeModel();
        }

        private void rd2_CheckedChanged(object sender, EventArgs e)
        {
            txtNameSpace.Text = "com.ErpCoreModel." + m_sNamespace;
            MakeModel();
        }
        private void rd3_CheckedChanged(object sender, EventArgs e)
        {
            txtNameSpace.Text = "com.ErpCoreModel." + m_sNamespace;
            MakeModel();
        }

        private void rd4_CheckedChanged(object sender, EventArgs e)
        {
            txtNameSpace.Text = "" ;
            MakeModel();
        }

        #region C#模板
        string sTemplete = @"// File:    {0}.cs
// Author:  甘孝俭
// email:   ganxiaojian@hotmail.com 
// QQ:      154986287
// http://www.8088net.com
// 协议声明：本软件为开源系统，遵循国际开源组织协议。任何单位或个人可以使用或修改本软件源码，
//          可以用于作为非商业或商业用途，但由于使用本源码所引起的一切后果与作者无关。
//          未经作者许可，禁止任何企业或个人直接出售本源码或者把本软件作为独立的功能进行销售活动，
//          作者将保留追究责任的权利。
// Created: {1}
// Purpose: Definition of Class {0}

using System;
using System.Text;
using ErpCoreModel.Framework;

namespace {2}
{{
    
    public class {0} : CBaseObject
    {{

        public {0}()
        {{
            TbCode = ""{3}"";
            ClassName = ""{2}.{0}"";

        }}

        {4}
    }}
}}";
        string sTempleteMgr = @"// File:    {0}Mgr.cs
// Author:  甘孝俭
// email:   ganxiaojian@hotmail.com 
// QQ:      154986287
// http://www.8088net.com
// 协议声明：本软件为开源系统，遵循国际开源组织协议。任何单位或个人可以使用或修改本软件源码，
//          可以用于作为非商业或商业用途，但由于使用本源码所引起的一切后果与作者无关。
//          未经作者许可，禁止任何企业或个人直接出售本源码或者把本软件作为独立的功能进行销售活动，
//          作者将保留追究责任的权利。
// Created: {1}
// Purpose: Definition of Class {0}Mgr

using System;
using System.Text;
using System.Collections.Generic;
using System.Reflection;
using ErpCoreModel.Framework;

namespace {2}
{{
    public class {0}Mgr : CBaseObjectMgr
    {{

        public {0}Mgr()
        {{
            TbCode = ""{3}"";
            ClassName = ""{2}.{0}"";
            Assembly assembly = Assembly.LoadFrom(Assembly.GetExecutingAssembly().Location);
            ObjType = assembly.GetType(ClassName);
        }}

    }}
}}";

        string sTAttr1 = @"
        public string {0}
        {{
            get
            {{
                if (m_arrNewVal.ContainsKey(""{1}""))
                    return m_arrNewVal[""{1}""].StrVal;
                else
                    return """";
            }}
            set
            {{
                if (m_arrNewVal.ContainsKey(""{1}""))
                    m_arrNewVal[""{1}""].StrVal = value;
                else
                {{
                    CValue val = new CValue();
                    val.StrVal = value;
                    m_arrNewVal.Add(""{1}"", val);
                }}
            }}
        }}";
        string sTAttr2 = @"
        public Guid {0}
        {{
            get
            {{
                if (m_arrNewVal.ContainsKey(""{1}""))
                    return m_arrNewVal[""{1}""].GuidVal;
                else
                    return Guid.Empty;
            }}
            set
            {{        
                if (m_arrNewVal.ContainsKey(""{1}""))
                    m_arrNewVal[""{1}""].GuidVal = value;
                else
                {{
                    CValue val = new CValue();
                    val.GuidVal = value;
                    m_arrNewVal.Add(""{1}"", val);
                }}
            }}
        }}";
        string sTAttr3 = @"
        public int {0}
        {{
            get
            {{
                if (m_arrNewVal.ContainsKey(""{1}""))
                    return m_arrNewVal[""{1}""].IntVal;
                else
                    return 0;
            }}
            set
            {{           
                if (m_arrNewVal.ContainsKey(""{1}""))
                    m_arrNewVal[""{1}""].IntVal = value;
                else
                {{
                    CValue val = new CValue();
                    val.IntVal = value;
                    m_arrNewVal.Add(""{1}"", val);
                }}
            }}
        }}";
        string sTAttr4 = @"
        public long {0}
        {{
            get
            {{
                if (m_arrNewVal.ContainsKey(""{1}""))
                    return m_arrNewVal[""{1}""].LongVal;
                else
                    return 0;
            }}
            set
            {{
                if (m_arrNewVal.ContainsKey(""{1}""))
                    m_arrNewVal[""{1}""].LongVal = value;
                else
                {{
                    CValue val = new CValue();
                    val.LongVal = value;
                    m_arrNewVal.Add(""{1}"", val);
                }}
            }}
        }}";
        string sTAttr5 = @"
        public double {0}
        {{
            get
            {{
                if (m_arrNewVal.ContainsKey(""{1}""))
                    return m_arrNewVal[""{1}""].DoubleVal;
                else
                    return 0;
            }}
            set
            {{
                if (m_arrNewVal.ContainsKey(""{1}""))
                    m_arrNewVal[""{1}""].DoubleVal = value;
                else
                {{
                    CValue val = new CValue();
                    val.DoubleVal = value;
                    m_arrNewVal.Add(""{1}"", val);
                }}
            }}
        }}";
        string sTAttr6 = @"
        public bool {0}
        {{
            get
            {{
                if (m_arrNewVal.ContainsKey(""{1}""))
                    return m_arrNewVal[""{1}""].BoolVal;
                else
                    return false;
            }}
            set
            {{
                if (m_arrNewVal.ContainsKey(""{1}""))
                    m_arrNewVal[""{1}""].BoolVal = value;
                else
                {{
                    CValue val = new CValue();
                    val.BoolVal = value;
                    m_arrNewVal.Add(""{1}"", val);
                }}
            }}
        }}";
        string sTAttr7 = @"
        public DateTime {0}
        {{
            get
            {{
                if (m_arrNewVal.ContainsKey(""{1}""))
                    return m_arrNewVal[""{1}""].DatetimeVal;
                else
                    return DateTime.Now;
            }}
            set
            {{
                if (m_arrNewVal.ContainsKey(""{1}""))
                    m_arrNewVal[""{1}""].DatetimeVal = value;
                else
                {{
                    CValue val = new CValue();
                    val.DatetimeVal = value;
                    m_arrNewVal.Add(""{1}"", val);
                }}
            }}
        }}";
        string sTAttr8 = @"
        public object {0}
        {{
            get
            {{
                if (m_arrNewVal.ContainsKey(""{1}""))
                    return m_arrNewVal[""{1}""].ObjectVal;
                else
                    return null;
            }}
            set
            {{
                if (m_arrNewVal.ContainsKey(""{1}""))
                    m_arrNewVal[""{1}""].ObjectVal = value;
                else
                {{
                    CValue val = new CValue();
                    val.ObjectVal = value;
                    m_arrNewVal.Add(""{1}"", val);
                }}
            }}
        }}";
        #endregion C#模板

        #region java模板
        string sTemplete_java = @"// File:    {0}.java
// Author:  甘孝俭
// email:   ganxiaojian@hotmail.com 
// QQ:      154986287
// http://www.8088net.com
// 协议声明：本软件为开源系统，遵循国际开源组织协议。任何单位或个人可以使用或修改本软件源码，
//          可以用于作为非商业或商业用途，但由于使用本源码所引起的一切后果与作者无关。
//          未经作者许可，禁止任何企业或个人直接出售本源码或者把本软件作为独立的功能进行销售活动，
//          作者将保留追究责任的权利。
// Created: {1}
// Purpose: Definition of Class {0}

package {2};

import java.lang.reflect.Type;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;
import com.ErpCoreModel.Framework.CBaseObject;
import com.ErpCoreModel.Framework.Util;
import com.ErpCoreModel.Framework.CValue;

public class {0} extends CBaseObject
{{

    public {0}()
    {{
        TbCode = ""{3}"";
        ClassName = ""{2}.{0}"";

    }}

    {4}
}}
";
        string sTempleteMgr_java = @"// File:    {0}Mgr.java
// Author:  甘孝俭
// email:   ganxiaojian@hotmail.com 
// QQ:      154986287
// http://www.8088net.com
// 协议声明：本软件为开源系统，遵循国际开源组织协议。任何单位或个人可以使用或修改本软件源码，
//          可以用于作为非商业或商业用途，但由于使用本源码所引起的一切后果与作者无关。
//          未经作者许可，禁止任何企业或个人直接出售本源码或者把本软件作为独立的功能进行销售活动，
//          作者将保留追究责任的权利。
// Created: {1}
// Purpose: Definition of Class {0}Mgr

package {2};

import com.ErpCoreModel.Framework.CBaseObjectMgr;

public class {0}Mgr extends CBaseObjectMgr
{{

    public {0}Mgr()
    {{
        TbCode = ""{3}"";
        ClassName = ""{2}.{0}"";
    }}

}}
";

        string sTAttr1_java = @"
        public String get{0}()
        {{
            if (m_arrNewVal.containsKey(""{1}""))
                return m_arrNewVal.get(""{1}"").StrVal;
            else
                return """";
        }}
        public void set{0}(String value)
        {{
            if (m_arrNewVal.containsKey(""{1}""))
                m_arrNewVal.get(""{1}"").StrVal = value;
            else
            {{
                CValue val = new CValue();
                val.StrVal = value;
                m_arrNewVal.put(""{1}"", val);
            }} 
       }}";     

        string sTAttr2_java = @"
        public UUID get{0}()
        {{
            if (m_arrNewVal.containsKey(""{1}""))
                return m_arrNewVal.get(""{1}"").GuidVal;
            else
                return Util.GetEmptyUUID();
        }}
        public void set{0}(UUID value)
        {{
            if (m_arrNewVal.containsKey(""{1}""))
                m_arrNewVal.get(""{1}"").GuidVal = value;
            else
            {{
                CValue val = new CValue();
                val.GuidVal = value;
                m_arrNewVal.put(""{1}"", val);
            }}
        }}";         

        string sTAttr3_java = @"
        public int get{0}()
        {{
            if (m_arrNewVal.containsKey(""{1}""))
                return m_arrNewVal.get(""{1}"").IntVal;
            else
                return 0;
        }}
        public void set{0}(int value)
        {{
            if (m_arrNewVal.containsKey(""{1}""))
                m_arrNewVal.get(""{1}"").IntVal = value;
            else
            {{
                CValue val = new CValue();
                val.IntVal = value;
                m_arrNewVal.put(""{1}"", val);
            }}
        }}"; 
        
        string sTAttr4_java = @"
        public long get{0}()
        {{
            if (m_arrNewVal.containsKey(""{1}""))
                return m_arrNewVal.get(""{1}"").LongVal;
            else
                return 0;
        }}
        public void set{0}(long value)
        {{
            if (m_arrNewVal.containsKey(""{1}""))
                m_arrNewVal.get(""{1}"").LongVal = value;
            else
            {{
                CValue val = new CValue();
                val.LongVal = value;
                m_arrNewVal.put(""{1}"", val);
            }}
        }}"; 
       
        string sTAttr5_java = @"
        public double get{0}()
        {{
            if (m_arrNewVal.containsKey(""{1}""))
                return m_arrNewVal.get(""{1}"").DoubleVal;
            else
                return 0;
        }}
        public void set{0}(double value)
        {{
            if (m_arrNewVal.containsKey(""{1}""))
                m_arrNewVal.get(""{1}"").DoubleVal = value;
            else
            {{
                CValue val = new CValue();
                val.DoubleVal = value;
                m_arrNewVal.put(""{1}"", val);
            }}
        }}"; 
       
        string sTAttr6_java = @"
        public Boolean get{0}()
        {{
            if (m_arrNewVal.containsKey(""{1}""))
                return m_arrNewVal.get(""{1}"").BoolVal;
            else
                return false;
        }}
        public void set{0}(Boolean value)
        {{
            if (m_arrNewVal.containsKey(""{1}""))
                m_arrNewVal.get(""{1}"").BoolVal = value;
            else
            {{
                CValue val = new CValue();
                val.BoolVal = value;
                m_arrNewVal.put(""{1}"", val);
            }}
        }}";
        
        string sTAttr7_java = @"
        public Date get{0}()
        {{
    	    if (m_arrNewVal.containsKey(""{1}""))
                return m_arrNewVal.get(""{1}"").DatetimeVal;
            else
                return new Date(System.currentTimeMillis());
        }}
        void set{0}(Date value)
        {{      
            if (m_arrNewVal.containsKey(""{1}""))
                m_arrNewVal.get(""{1}"").DatetimeVal=value;
            else
            {{
                CValue val = new CValue();
                val.DatetimeVal= value;
                m_arrNewVal.put(""{1}"", val);
            }}
        }}";
        
        string sTAttr8_java = @"
        public Object get{0}()
        {{
    	    if (m_arrNewVal.containsKey(""{1}""))
                return m_arrNewVal.get(""{1}"").ObjectVal;
            else
                return null;
        }}
        void set{0}(Object value)
        {{      
            if (m_arrNewVal.containsKey(""{1}""))
                m_arrNewVal.get(""{1}"").ObjectVal=value;
            else
            {{
                CValue val = new CValue();
                val.ObjectVal= value;
                m_arrNewVal.put(""{1}"", val);
            }}
        }}";
        
        #endregion java模板

    }
}
